﻿using PMSystem.DataAccess.DataContext;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PMSystem.DataAccess.DataRepository
{
    public class RecordItemRepository : IRepository<RecordItem>
    {
        Context context = new Context();

        public IQueryable<RecordItem> GetAll
        {
            get { return context.RecordItems; }
        }

        public IQueryable<RecordItem> AllIncluding(params Expression<Func<RecordItem, object>>[] includeProperties)
        {
            IQueryable<RecordItem> query = context.RecordItems;
            foreach (var includeProperty in includeProperties)
            {
                // query = query.Include(includeProperty);
            }
            return query;
        }



        public void InsertOrUpdate(RecordItem record)
        {
            if (record.RecordId == default(int))
            {
                // New entity
                context.RecordItems.Add(record);
            }
            else
            {
                // Existing entity
                context.Entry(record).State = EntityState.Modified;
            }
            
        }

        public void Delete(int id)
        {
            var employee = context.RecordItems.Find(id);
            context.RecordItems.Remove(employee);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose()
        {
            context.Dispose();
        }

        public RecordItem GetById(int id)
        {
            return context.RecordItems.Find(id);
        }


        public List<RecordItem> GetAllByCondition(Expression<Func<RecordItem, bool>> predicate)
        {
            return context.RecordItems.Where(predicate).ToList();
        }
    }
}
